package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.StrategyQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.CheckLoginAnnotation;
import cn.wolfcode.luowowo.website.web.annotation.GetUserInfoAnnotation;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisVOService iStrategyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;
    @RequestMapping("")
    //@CheckLoginAnnotation
    public String index(Long id, Model model){
        List<StrategyStatisVO> abroadCds = new ArrayList();
        List<StrategyStatisVO> unabroadCds = new ArrayList();
        List<StrategyStatisVO> vos = iStrategyStatisVOService.getHowManyTopByStatis(StrategyStatisVO.ABBROADTYPE,-1);
        for (StrategyStatisVO vo : vos) {
            if (vo.isIsabroad()){
                //true为国外
                //abroadCds 国外排行
                if(abroadCds.size()<10) {
                    abroadCds.add(vo);
                }
            }else {
                //unabroadCds 国内排行
                if(unabroadCds.size()<10) {
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);
        //hotCds 热度排行
        List<StrategyStatisVO> hotTop10 = iStrategyStatisVOService.getHowManyTopByStatis(StrategyStatisVO.HOTTYPE, 10-1);
        model.addAttribute("hotCds",hotTop10);
        //themeCds 主题排行

        //commends 推荐top5
        List<StrategyCommend> list = iStrategyStatisVOService.getDetailHotTop5();
        model.addAttribute("commends",list);

        //themeCds
        List<Map<String,Object>> themeCds = strategySearchService.getStrategyThemeTop5();
        model.addAttribute("themeCds",themeCds);
        //chinas
        List<StatisVO> chinas = strategySearchService.queryConditionChinasGroup();
        model.addAttribute("chinas",chinas);
        //abroads
        List<StatisVO> abroads = strategySearchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);
        //themes
        List<StatisVO> themes = strategySearchService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);
        return "strategy/index";
    }
    @RequestMapping("detail")
    //@CheckLoginAnnotation
    public String detail(Long id, Model model){
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail",detail);
        StrategyStatisVO statisVONums = iStrategyStatisVOService.getStatisVONums(id);
        model.addAttribute("vo",statisVONums);
        iStrategyStatisVOService.viewNumIncrease(id,1);

        //热门外排行中阅读数加一(分数+1)
        iStrategyStatisVOService.increaseCdsScores(detail.getId(),1,StrategyStatisVO.HOTTYPE);
        return "strategy/detail";
    }
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags",tags);
        //pageInfo
        PageInfo<StrategyDetail> pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategy/list";
    }


    @RequestMapping("commentAdd")
    @ResponseBody
    @CheckLoginAnnotation
    public Object commentAdd(StrategyComment comment, @GetUserInfoAnnotation UserInfo userInfo){
        //设置用户信息
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        strategyCommentService.save(comment);

        iStrategyStatisVOService.addReplynum(comment.getDetailId(),1);

        //热门外排行中评论数加一(分数+1)
        iStrategyStatisVOService.increaseCdsScores(comment.getDetailId(),1,StrategyStatisVO.HOTTYPE);


        //返回评论数replynum
        int replynum = iStrategyStatisVOService.getStatisVONums(comment.getDetailId()).getReplynum();
        return AjaxResult.SUCCESS.addData(replynum);


    }


    @RequestMapping("comment")
    public String comment(StrategyCommentQuery qo,Model model){
        //page
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }


     @RequestMapping("commentThumbUp")
     @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){
        strategyCommentService.addThumbUp(toid,fromid);
        //这里的返回值无意义
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid,@GetUserInfoAnnotation UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "您还没登录");
            result.setCode(102);
            return result;
        }
        boolean b = iStrategyStatisVOService.addFavor(sid,userInfo.getId());

        //国内外排行中收藏数加一(分数+1)
        iStrategyStatisVOService.increaseCdsScores(sid,1,StrategyStatisVO.ABBROADTYPE);

        return new AjaxResult(b,null).addData(iStrategyStatisVOService.getStatisVONums(sid));
    }

    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@GetUserInfoAnnotation UserInfo userInfo){
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "您还没登录");
            result.setCode(102);
            return result;
        }
        boolean b = iStrategyStatisVOService.addStrategyThumbup(sid,userInfo.getId());


        //国内外排行中点赞数加一(分数+1)
        iStrategyStatisVOService.increaseCdsScores(sid,1,StrategyStatisVO.ABBROADTYPE);

        return new AjaxResult(b,null).addData(iStrategyStatisVOService.getStatisVONums(sid));
    }


    @RequestMapping("searchPage")
    public String searchPage(SearchQueryObject qo,Model model){
        Page page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }
}
